package com.xyongfeng.foodManagement.config;


import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.CacheWriter;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.xyongfeng.foodManagement.domain.FoodInfo;
import com.xyongfeng.foodManagement.domain.Permission;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.StampedLock;

@Configuration
@EnableCaching
public class CaffeineConfig {


    @Bean
    public Cache<Integer, FoodInfo> foodCache() {
        return Caffeine.newBuilder()
                // 10秒没有访问就过期
                .expireAfterAccess(10, TimeUnit.SECONDS)
                .build();
    }


    @Bean
    public Cache<Integer, List<Permission>> adminPermCache() {
        return Caffeine.newBuilder()
                // 3秒没有访问就过期
                .expireAfterWrite(3, TimeUnit.SECONDS)
                .maximumSize(100)
                .build();
    }

}
